﻿package com.singlenotion.effects 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.utils.setTimeout;
	import gs.easing.Quint;
	import gs.TweenGroup;
	import gs.TweenMax;
	
	/**
	 * ...
	 * @author Adam Jowett (Single Notion)
	 */
	public class Bubbles extends Sprite
	{
		public static const UP:String = "moveUp";
		public static const RIGHT:String = "moveRight";
		public static const DOWN:String = "moveDown";
		public static const LEFT:String = "moveLeft";
		public static const UP_RIGHT:String = "moveUpRight";
		public static const DOWN_RIGHT:String = "moveDownRight";
		public static const UP_LEFT:String = "moveUpLeft";
		public static const DOWN_LEFT:String = "moveDownLeft";
		public static const SURROUND:String = "sourround";
		
		private var _totalBubbles:int;
		private var _maxRadius:Number;
		private var _speed:Number;
		private var _maxdistanceX:Number;
		private var _direction:String;
		private var _bubbles:Array;
		private var _returnToOrigin:Boolean;
		private var _maxdistanceY:Number;
		private var _destroyOnComplete:Boolean;
		private var _minDistance:Number = 50;
		private var _asset:Class;
		
		public function Bubbles($asset:Class = null, $totalBubbles:int = 25, $maxRadius:Number = 5, $speed:Number = 5, $maxdistanceX:Number = 55, 
								$maxdistanceY:Number = 55, $direction:String = SURROUND, $returnToOrigin:Boolean = true, 
								$destroyOnComplete:Boolean = false) 
		{
			_asset = $asset;
			_totalBubbles = $totalBubbles;
			_maxRadius = $maxRadius;
			_speed = $speed;
			_maxdistanceX = $maxdistanceX;
			_maxdistanceY = $maxdistanceY;
			_direction = $direction;
			_returnToOrigin = $returnToOrigin;
			_destroyOnComplete = $destroyOnComplete;
			
			init();
		}
		
		private function init():void
		{
			mouseChildren = false;
			mouseEnabled = false;
				
			_bubbles = new Array();
			createBubbles();
		}
		
		protected function createBubbles():void
		{
			var i;
			for (i = 0; i < _totalBubbles; i++) {
				drawBubble(0xFFFFFF * Math.random(), _maxRadius * Math.random());
			}
		}
		
		protected function drawBubble($colour:int, $radius:Number):void
		{
			var bubble:*;
			if (_asset == null) 
			{
				bubble = new Sprite();
				bubble.graphics.beginFill($colour, 1);
				bubble.graphics.drawCircle(0, 0, $radius);
				bubble.graphics.endFill();
			} else {
				bubble = new _asset();
			}
			
			//bubble.alpha = 0;
			
			addChild(bubble);
			animateBubble(bubble);
			
			_bubbles.push(bubble);
		}
		
		protected function animateBubble($bubble:Sprite)
		{
			var targetX:Number;
			var targetY:Number;
			
			switch (_direction) {
				case DOWN_RIGHT:
				targetX = _maxdistanceX * Math.random();
				targetY = _maxdistanceY * Math.random();
				break;
				case UP_RIGHT:
				targetX = _maxdistanceX * Math.random();
				targetY = -(_maxdistanceY * Math.random());
				break;
				case DOWN_LEFT:
				targetX = -(_maxdistanceX * Math.random());
				targetY = _maxdistanceY * Math.random();
				break;
				case UP_LEFT:
				targetX = -(_maxdistanceX * Math.random());
				targetY = -(_maxdistanceY * Math.random());
				break;
				case UP:
				targetX = -_maxdistanceX * Math.random() + _maxdistanceX * Math.random();
				targetY = -(_maxdistanceY * Math.random());
				break;
				case DOWN:
				targetX = -_maxdistanceX * Math.random() + _maxdistanceX * Math.random();
				targetY = _maxdistanceY * Math.random();
				break;
				case RIGHT:
				targetX = _maxdistanceX * Math.random();
				targetY = -_maxdistanceY * Math.random() + _maxdistanceY * Math.random();
				break;
				case LEFT:
				targetX = -(_maxdistanceX * Math.random());
				targetY = -_maxdistanceY * Math.random() + _maxdistanceY * Math.random();
				break;
				case SURROUND:
				targetX = -_maxdistanceX * Math.random() + _maxdistanceX * Math.random();
				targetY = -_maxdistanceY * Math.random() + _maxdistanceY * Math.random();
				break;
			}
			
			tween($bubble, targetX, targetY);
		}
		
		protected function tween($bubble:Sprite, $targetX:Number, $targetY:Number):void
		{
			$bubble.alpha = 1;
			if (_returnToOrigin) {
				TweenMax.to($bubble, _speed * Math.random(), { x:String($targetX), y:String($targetY), alpha:0, scaleX:0, scaleY:0, yoyo:1, loop:0, onComplete:checkDestroy } );
			} else {
				TweenMax.to($bubble, _speed * Math.random(), { x:String($targetX), y:String($targetY), ease:Quint.easeOut, alpha:0, scaleX:0, scaleY:0, loop:0, onComplete:checkDestroy } );
			}
		}
		
		protected function checkDestroy():void
		{
			if (_destroyOnComplete) destroy();
		}
		
		protected function destroy():void
		{
			for each (var bubble:Sprite in _bubbles) {
				TweenMax.killTweensOf(bubble);
				removeChild(bubble);
			}
			
			if (parent != null) parent.removeChild(this);
		}
	}
}